package org.cp.dao.user.impl;

import java.util.List;

import org.cp.dao.BaseDAO;
import org.cp.dao.user.IUserDAO;
import org.cp.vo.User;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class UserDAO extends BaseDAO implements IUserDAO {

	public void changeUserInfo(User user) {
		Session session = getSession();
		Transaction tx = session.beginTransaction();
		session.update(user);
		tx.commit();
		session.close();

	}

	public User login(String accounts, String passWord) {
		Session session = getSession();
		String hql = "from User where accounts=? and password=?";
		Query query = session.createQuery(hql);
		query.setParameter(0,accounts);
		query.setParameter(1,passWord);
		List users = query.list();
		if(users.size() != 0){
			
			User user = (User)users.get(0);
			session.close();
			return user;
		}
		session.close();
		return null;
	}

	public void logout() {
		// TODO Auto-generated method stub

	}

	public void register(User user) {
		Session session = getSession();
		Transaction tx = session.beginTransaction();
		session.save(user);
		tx.commit();
		session.close();

	}
	
	public User searchUserByAccounts(String accounts) {
		Session session = getSession();
		String hql = "from User where accounts=?";
		Query query = session.createQuery(hql);
		query.setParameter(0,accounts);
		List users = query.list();
		if(users.size() != 0){
			
			User user = (User)users.get(0);
			session.close();
			return user;
		}
		session.close();
		return null;
	}

}
